Skip to main content

i18n

The i18n Service is responsible of the Internationalization and localization of the application.

Details

The service is created based on the cross-platform definition for Elevate with an small change: an init method is included in order to initialize the i18n system of the application.

The default i18n provider is Accedo Control, but Elevate includes also a local provider that used the same Format than the Accedo Control one, in order to help with the tests, but also to help developers of new project to switch to this model in case that no other is available.

Besides the standard Service/Provider/Model architecture, the i18n is provided with a React Provider + Hook that allows developers to use the Service methods and listen to i18n value changes (currentLocale, locales, ...).

Available methods:

  • init(): initialize the entire i18n flow, including set the initial locale.
  • getLocales(): get all the available locale for the current Application
  • setLocale(localeCode): set the current locale, retrieving the dictionary if needed.
  • getLocale(): gets the current locale of the application.
  • getTranslation(key, options): gets a translation value based on a key and an options object to allow interpolation and pluralization.

The i18n Service uses node-polyglot under to hood in order to set the dictionary and get the translations.

Associated Models

The i18n Service has two different models associated:

  • locale: For the locale definition as defined in Accedo Control
  • dictionary: For the combination of dictionary entries/phrases and the directionality. Includes
    • code: the locale code.
    • dir: the directionality ('ltr' or 'rtl').
    • entries: the translation key/pairs.

i18n hook

The i18n hook #/hooks/useI18n exposes the following to the application from the Context:

  • isInitiated: Check weather the i18n system is already initiated.
  • currentLocale: the current locale (code + dir) of the application.
  • locales: the current set of locales of the application.
  • init: a method to init the i18n system.
  • setInitiated: a method to change the initiated state of the i18n Context.
  • setCurrentLocale: a method to set the current locale state of the i18n Context.
  • setLocales: a method to set the locales state of the i18n Context.

Example usage

import { getTranslation } from '#/services/i18n';
[...]
getTranslation('username');